package com.xdja.jwt.imagecollect.mvp.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.jess.arms.base.BaseFragment
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.xdja.jwt.imagecollect.R
import com.xdja.jwt.imagecollect.app.adapter.UploadingAdapter
import com.xdja.jwt.imagecollect.app.bean.FolderBean
import com.xdja.jwt.imagecollect.app.db.entity.UploadFileBean
import com.xdja.jwt.imagecollect.app.ext.setGrayVerticalDivider
import com.xdja.jwt.imagecollect.app.utils.DiffUploadCallBack
import com.xdja.jwt.imagecollect.app.widget.PATH
import com.xdja.jwt.imagecollect.di.component.DaggerUploadingComponent
import com.xdja.jwt.imagecollect.di.module.UploadingModule
import com.xdja.jwt.imagecollect.mvp.contract.UploadingContract
import com.xdja.jwt.imagecollect.mvp.presenter.UploadingPresenter
import kotlinx.android.synthetic.main.fragment_uploading.*


/**
 * @author yuanwanli
 * @date 08/27/2019
 * @des  正在上传文件列表
 */
const val FOLDERID = "FolderId"
const val FOLDERNAME = "FolderName"

class UploadingFragment : BaseFragment<UploadingPresenter>(), UploadingContract.View {
    private var isFirst: Boolean = true
    override fun showLoadingBean(beans: List<UploadFileBean>) {
        if (isFirst) {
            beans.forEach {
                mPresenter?.uploadBean(it)
            }
            isFirst = false
        }
        val callback = DiffUploadCallBack(beans)
        uploadAdapter.setNewDiffData(callback)
    }

    lateinit var uploadAdapter: UploadingAdapter

    companion object {
        fun newInstance(path: ArrayList<String>?, bean: FolderBean?): UploadingFragment {
            val fragment = UploadingFragment()
            var bundle = Bundle()
            bundle.putStringArrayList(PATH, path)
            bean?.let {
                bundle.putString(FOLDERID, bean.id)
                bundle.putString(FOLDERNAME, bean.name)
            }
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun setupFragmentComponent(appComponent: AppComponent) {
        DaggerUploadingComponent //如找不到该类,请编译一下项目
            .builder()
            .appComponent(appComponent)
            .uploadingModule(UploadingModule(this))
            .build()
            .inject(this)
    }

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_uploading, container, false);
    }

    override fun initData(savedInstanceState: Bundle?) {
        val foldId = arguments?.getString(FOLDERID)
        val foldName = arguments?.getString(FOLDERNAME)
        val arrayList = arguments?.getStringArrayList(PATH)
        recyclerView.layoutManager = LinearLayoutManager(context)
        uploadAdapter = UploadingAdapter(null)
        (recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        recyclerView.adapter = uploadAdapter
        recyclerView.setGrayVerticalDivider(context!!, 1f, R.color.line_bg)
        uploadAdapter.setOnItemChildClickListener { _, _, position ->
            val item = uploadAdapter.getItem(position)
            item?.let {
                if (it.hasStart) {
                    mPresenter?.stopUploadBean(it)
                } else {
                    mPresenter?.uploadBean(it)
                }
            }
            uploadAdapter.notifyItemChanged(position)
        }
        if (arrayList != null && foldId != null && foldName != null) {
            mPresenter?.insertBean(arrayList, foldId, foldName)
        }
        mPresenter?.getAllLoadingUploadBean()
    }

    override fun setData(data: Any?) {

    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {

    }

    override fun onDestroy() {
        super.onDestroy()
        isFirst = true
    }
}




